//
//  BanquetHall.swift
//  FeastBooking
//
//  Created by Imon on 2017/9/9.
//  Copyright © 2017年 Imon. All rights reserved.
//

import UIKit

protocol BanquetHallViewDelegate {
    func showBanquetHall(banquetHall: RestaurantBanquetHall)
}

class BanquetHallView: UIView {
    
    var delegate: BanquetHallViewDelegate?
    
    var banquetHall: RestaurantBanquetHall?
    
    let profilePic: UIImageView = {
        let imageView = UIImageView(image: UIImage(named: "placeholder"))
        imageView.contentMode = .scaleAspectFill
        imageView.layer.masksToBounds = true
        return imageView
    }()
    
    let name: UILabel = {
        let label = UILabel()
        label.textColor = UIColor.gray
        label.textAlignment = .left
        label.font = AppFont(size: 15)
        label.numberOfLines = 0
        label.text = LanguageHelper.getString(key: "NO_RESULT")
        return label
    }()
    
    let tables: UILabel = {
        let label = UILabel()
        label.textColor = UIColor.lightGray
        label.font = AppFont(size: 12)
        label.text = LanguageHelper.getString(key: "NO_RESULT")
        return label
    }()
    
    let area: UILabel = {
        let label = UILabel()
        label.textColor = UIColor.lightGray
        label.font = AppFont(size: 12)
        label.text = LanguageHelper.getString(key: "NO_RESULT")
        return label
    }()
    
    let minimumConsumption: UILabel = {
        let label = UILabel()
        label.textColor = UIColor.lightGray
        label.font = AppFont(size: 12)
        label.text = LanguageHelper.getString(key: "NO_RESULT")
        return label
    }()
    
    let defaultImage = UIImage(named: "loading_reco")
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = UIColor.white
        self.layer.cornerRadius = 5
        self.layer.borderColor = UIColor.lightGray.cgColor
        self.layer.borderWidth = 0.5
        self.layer.masksToBounds = true
        self.addSubview(profilePic)
        self.addSubview(name)
        self.addSubview(tables)
        self.addSubview(area)
        self.addSubview(minimumConsumption)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        if touches.first != nil {
            delegate?.showBanquetHall(banquetHall: banquetHall!)
        }
    }
    
    func initValue(banquetHall: RestaurantBanquetHall) {
        self.banquetHall = banquetHall
        
        if let urlS = banquetHall.pictureUrl {
            if urlS != "" {
                let url = URL(string: urlS)
                profilePic.kf.setImage(with: url, placeholder: defaultImage)
            } else {
                profilePic.image = defaultImage
            }
        }
        self.name.text = banquetHall.name
        if let area = banquetHall.area {
            self.area.text = "\(LanguageHelper.getString(key: "AREA_SHORT")) \(area) ㎡"
        }
        if let tableRange = banquetHall.tableRange {
            self.tables.text = "\(tableRange[0])-\(tableRange[1])\(LanguageHelper.getString(key: "TABLE"))"
        }
        if let min = banquetHall.minimunPrice {
            self.minimumConsumption.text = "\(LanguageHelper.getString(key: "MIN_CONSUMPTION")) $\(min)"
        }
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        let labelHeight: CGFloat = 20
//        let textMaxSize = CGSize(width: bounds.width, height: 20)
        profilePic.frame = UIEdgeInsetsInsetRect(bounds, UIEdgeInsetsMake(0, 0, 0, bounds.width * 2 / 3))
        let nameMaxSize = CGSize(width: (bounds.width * 2 / 3) - 10 , height: 80)
        let nameSize = TextSize.textSize(text: name.text!, font: name.font, maxSize: nameMaxSize)
        name.frame = UIEdgeInsetsInsetRect(bounds, UIEdgeInsetsMake(5, bounds.width * 1 / 3 + 10, bounds.height - 5 - nameSize.height - 5, 5))
//        let tablesWidth = TextSize.textSize(text: tables.text!, font: tables.font, maxSize: textMaxSize).width
//        let areaWidth = TextSize.textSize(text: area.text!, font: area.font, maxSize: textMaxSize).width
//        let minimumCWidth = TextSize.textSize(text: minimumConsumption.text!, font: minimumConsumption.font, maxSize: textMaxSize).width
        area.frame = UIEdgeInsetsInsetRect(bounds, UIEdgeInsetsMake(name.frame.origin.y + name.frame.height, bounds.width * 1 / 3 + 10, bounds.height - 5 - nameSize.height - 5 - labelHeight, 5))
        tables.frame = UIEdgeInsetsInsetRect(bounds, UIEdgeInsetsMake(name.frame.origin.y + name.frame.height + labelHeight, bounds.width * 1 / 3 + 10, bounds.height - 5 - nameSize.height - 5 - labelHeight * 2, 5))
        minimumConsumption.frame = UIEdgeInsetsInsetRect(bounds, UIEdgeInsetsMake(name.frame.origin.y + name.frame.height + labelHeight * 2, bounds.width * 1 / 3 + 10, bounds.height - 5 - nameSize.height - 5 - labelHeight * 3, 5))
    }
}

class BanquetHallBoard: UIView {
    
    let titleLabel: UILabel = {
        let label = UILabel()
        label.font = AppFont(size: 14)
        label.textColor = UIColor.black
        label.text = LanguageHelper.getString(key: "FEAST_HALL")
        return label
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = UIColor.white
        self.addSubview(titleLabel)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        let textMaxSize = CGSize(width: bounds.width, height: 20)
        let titleSize = TextSize.textSize(text: titleLabel.text!, font: titleLabel.font, maxSize: textMaxSize)
        titleLabel.frame = CGRect(x: 10, y: 10, width: titleSize.width, height: titleSize.height)
    }
}

class BHConfigurationView: UIView {
    
    let icon: UIImageView = {
        let imageView = UIImageView()
        imageView.contentMode = .scaleAspectFill
        imageView.layer.masksToBounds = true
        imageView.image = UIImage(named: "tick")
        return imageView
    }()
    
    let contentLabel: UILabel = {
        let label = UILabel()
        label.font = AppFont(size: 13)
        label.textColor = UIColor.lightGray
        return label
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = UIColor.white
        self.addSubview(icon)
        self.addSubview(contentLabel)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        icon.frame = CGRect(x: 0, y: 0, width: self.frame.height, height: self.frame.height)
        contentLabel.frame = CGRect(x: self.frame.height + 2, y: 1, width: self.frame.width - self.frame.height - 2, height: self.frame.height - 2)
    }
}
